#include "include.h"

using namespace std;

SDL_Surface* SDLGUIGame = NULL;
SDL_Surface* SDLGUIImageBlock = NULL;
SDL_Surface* SDLGUIImageBlock2 = NULL;
SDL_Surface* SDLGUIImageBlock3 = NULL;

char getUnicodeValue( const SDL_KeyboardEvent &key )
{
	const int INTERNATIONAL_MASK = 0xFF80;
	const int UNICODE_MASK = 0x7F;
	int uni = key.keysym.unicode;
	if( uni == 0 )
	{
		return (char)'\0';
	}
	return (char)uni;
}

void onGUIGameStart(SDL_Surface* ScreenGame)
{
	SDLGUIGame = ScreenGame;
	SDLGUIImageBlock = IMG_Load(".\\data\\block.png");
	SDLGUIImageBlock2 = IMG_Load(".\\data\\block2.png");
	SDLGUIImageBlock3 = IMG_Load(".\\data\\block3.png");
}

GUI_TextBox::GUI_TextBox(int x, int y, int sizex, int sizey)
{
	guix = x;
	guiy = y;
	guisizex = sizex;
	guisizey = sizey;
	guireadonly = false;
	guicreated = true;
	guicolor.r = 255;
	guicolor.g = 255;
	guicolor.b = 255;
	guimask = false;
	guifont = ".\\data\\fonts\\font2.ttf";
}

GUI_TextBox::~GUI_TextBox()
{
};

void GUI_TextBox::SetText(string text)
{
	guistring = text;
	guisec = 0;
}

void GUI_TextBox::SetColor(int red, int green, int blue)
{
	guicolor.r = red;
	guicolor.g = green;
	guicolor.b = blue;
}

void GUI_TextBox::SetFont(string filename)
{
	ifstream file (filename.c_str());
	if (file.is_open())
	{
		guifont = filename;
		file.close();
	}
	else
	{
		guifont = ".\\data\\fonts\\font2.ttf";
	}
}

void GUI_TextBox::SetPos(int x, int y)
{
	guix = x;
	guiy = y;
}

void GUI_TextBox::Masking(bool toggle)
{
	guimask = toggle;
}

void GUI_TextBox::ReadOnly(bool toggle)
{
	guireadonly = toggle;
}

void GUI_TextBox::Toggle(bool toggle)
{
	guitoggle = toggle;
	guisec = 0;
}

void GUI_TextBox::Render()
{
	if (guicreated == true && guitoggle == true)
	{
		guisec += 100;
		int TextBoxXR;
		int TextBoxYR;
		for (TextBoxXR = 0; TextBoxXR < guisizex; TextBoxXR = TextBoxXR + 5)
		{
			for (TextBoxYR = 0; TextBoxYR < guisizey; TextBoxYR = TextBoxYR + 5)
			{
				drawImage(0, guix+TextBoxXR, guiy+TextBoxYR, SDLGUIImageBlock, SDLGUIGame, NULL);
				drawImage(0, guix+TextBoxXR, guiy, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix, guiy+TextBoxYR, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix+guisizex, guiy+TextBoxYR, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix+TextBoxXR, guiy+guisizey, SDLGUIImageBlock2, SDLGUIGame, NULL);

				// Fixed
				drawImage(0, guix+guisizex, guiy+guisizey, SDLGUIImageBlock2, SDLGUIGame, NULL);
			}
		}
		if (!guistring.length() == 0)
		{
			if (guimask == false)
			{
				drawText(guicolor.r, guicolor.g, guicolor.b, 255, guistring, guifont, "right", 32, guix+10, guiy+10 );
			}
			else
			{
				int guimaskedint = 0;
				string guimaskedstr;
				for (guimaskedint = 0; guimaskedint < guistring.length(); guimaskedint++)
				{
					guimaskedstr += "*";
				}
				drawText(guicolor.r, guicolor.g, guicolor.b, 255, guimaskedstr, guifont, "right", 32, guix+10, guiy+10 );
			}
		}
		if (guisec > 1000 && guiinput == true && guireadonly == false)
		{
			int TextBoxY2;
			for (TextBoxY2 = 0; TextBoxY2 < guisizey-10; TextBoxY2++)
			{
				if (!guistring.length() == 0)
				{
					drawImage(0, guix+GetFontWidth(guifont, guistring, 32)+10, guiy+TextBoxY2+8, SDLGUIImageBlock3, SDLGUIGame, NULL);
				}
				else 
				{ 
					drawImage(0, guix+10, guiy+TextBoxY2+8, SDLGUIImageBlock3, SDLGUIGame, NULL);
				}
			}
			if (guisec > 2000)
			{
				guisec = 0;
			}
		}
	}
}

void GUI_TextBox::Events(SDL_Event& events)
{
	switch (events.type)
	{
	case SDL_MOUSEBUTTONDOWN:
		{
			if (events.button.button == SDL_BUTTON_LEFT)
			{
 				if (events.motion.x > guix && events.motion.x < guix+guisizex && events.motion.y > guiy && events.motion.y < guiy+guisizey)
 				{
 					guiinput = true;
				}
 				else
 				{
 					guiinput = false;
					guisec = 0;
 				}
				
			}
			break;
		}
	case SDL_KEYDOWN:
		{
			switch ( events.key.keysym.sym )
			{
			case SDLK_BACKSPACE:
				if (guiinput == true && guireadonly == false)
				{
					if(!guistring.empty())
					{
						guistring.erase(guistring.end() - 1);
						guisec = 0;
					}
				}
				break;
			default:
				if (guiinput == true && guireadonly == false)
				{
					char key = getUnicodeValue(events.key);
					if(key != '\0')
					{
						guistring += key;
						guisec = 0;
					}
				}
				break;
			}
		}
	}
}

string GUI_TextBox::GetText()
{
	if (!guistring.length() == 0) { return guistring; }
	else { return "\0"; }
}

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// Button                                                               //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

GUI_Button::GUI_Button(string text, int x, int y, int sizex, int sizey)
{
	guix = x;
	guiy = y;
	guisizex = sizex;
	guisizey = sizey;
	guicreated = true;
	guicolor.r = 255;
	guicolor.g = 255;
	guicolor.b = 255;
	guistring = text;
	guifont = ".\\data\\fonts\\font2.ttf";
}

void GUI_Button::SetColor(int red, int green, int blue)
{
	guicolor.r = red;
	guicolor.g = green;
	guicolor.b = blue;
}

void GUI_Button::SetFont(string filename)
{
	ifstream file (filename.c_str());
	if (file.is_open())
	{
		guifont = filename;
		file.close();
	}
	else
	{
		guifont = ".\\data\\fonts\\font1.ttf";
	}
}

void GUI_Button::SetPos(int x, int y)
{
	guix = x;
	guiy = y;
}

void GUI_Button::Toggle(bool toggle)
{
	guitoggle = toggle;
}

void GUI_Button::Render()
{
	if (guicreated == true && guitoggle == true)
	{
		int TextBoxXR;
		int TextBoxYR;
		for (TextBoxXR = 0; TextBoxXR < guisizex; TextBoxXR = TextBoxXR + 5)
		{
			for (TextBoxYR = 0; TextBoxYR < guisizey; TextBoxYR = TextBoxYR + 5)
			{
				drawImage(0, guix+TextBoxXR, guiy+TextBoxYR, SDLGUIImageBlock, SDLGUIGame, NULL);
				drawImage(0, guix+TextBoxXR, guiy, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix, guiy+TextBoxYR, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix+guisizex, guiy+TextBoxYR, SDLGUIImageBlock2, SDLGUIGame, NULL);
				drawImage(0, guix+TextBoxXR, guiy+guisizey, SDLGUIImageBlock2, SDLGUIGame, NULL);

				// Fixed
				drawImage(0, guix+guisizex, guiy+guisizey, SDLGUIImageBlock2, SDLGUIGame, NULL);
			}
		}
		if (!guistring.length() == 0)
		{
			drawText(guicolor.r, guicolor.g, guicolor.b, 255, guistring, guifont, "center", 32, guix+guisizex/2, guiy+4 );
		}
	}
}

void GUI_Button::Events(SDL_Event& events)
{
	switch (events.type)
	{
	case SDL_MOUSEBUTTONDOWN:
		{
			if (events.button.button == SDL_BUTTON_LEFT && guitoggle == true)
			{
				if (events.motion.x > guix && events.motion.x < guix+guisizex && events.motion.y > guiy && events.motion.y < guiy+guisizey)
				{
					GUI_Button::onGUIClicked();
				}
			}
			break;
		}
	}
}

void GUI_Button::onGUIClicked()
{
	if (clicked)
	{
		clicked();
	}
}